package com.shop.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.shop.dao.impl.ShopDao;
import com.shop.model.Shop;


@WebServlet("/delShop")
public class delShopServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public delShopServlet() {
        super();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String parm = request.getParameter("key");
		String account = (String) request.getSession().getAttribute("account");
		System.out.println(account+":"+parm);
		ShopDao shopDao = new ShopDao();
		if (parm != null) {
			//根据商品名称与account删除数据库记录
			Integer d = shopDao.delete(parm,account);
			if (d != null ) {
				System.out.println("账号:"+account+"-"+parm+"数据删除成功");
			} else {
				System.out.println("账号:"+account+"-"+parm+"数据删除失败");
			}
		}else {
			//清空当前账号的购物车
			Integer d = shopDao.delShop(account);
			if (d != null ) {
				System.out.println("账号:"+account+":"+"数据清空成功");
			} else {
				System.out.println("账号:"+account+":"+"数据清空失败");
			}
		}
		
		List<Shop> list = shopDao.selShoppingCar(account);
		request.setAttribute("list", list);		
		// 跳转
		request.getRequestDispatcher("shoppingCar.jsp").forward(request, response);			
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
